<?php

namespace app\controller\admin\map;

use app\model\admin\calendar\CalendarContent;
use app\model\admin\map\MapBoard;
use app\model\admin\map\MapBoardContent;
use app\model\admin\map\MapContent;
use app\model\admin\map\MapContentLabel;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Career
 * @package app\controller\admin\map
 */
class BoardContent extends Backend
{
    protected $model;
    public $orderRule = ['sort'=>'ASC', 'id' => 'DESC'];
    protected $noNeedAuth = ['index', 'info'];
    public function initialize()
    {
        $this->model = new MapBoardContent();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    //查看详情
    public function info()
    {
        $id = $this->request->param('id');
        $info = $this->model->find($id);
        return $this->success('获取成功', $info);
    }

    public function index()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        } else {
            $limit = $this->request->param('limit', 10);
            $param = $this->request->param();
            if(isset($param['search_param']['title']['value'])&&!empty($param['search_param']['title']['value'])){
                $contentIds = (new MapContent())->whereLike('title',$param['search_param']['title']['value'])->column("id");
                $where[] = [
                    'content_id','in',$contentIds
                ];
            }
            $data = $this->model->hasWhere("content",function ($query){
                return $query->whereNotNull("id");
            })->with("content")->where($where)->order($order)->paginate($limit)->toArray();
            foreach ($data['data'] as &$item) {
                if(isset($item['content'])) $item['name'] = $item['content']['title'];
            }
        }
        return $this->success('数据获取成功', $data);
    }

    //添加
    public function add()
    {
        $post = CommonFun::filterPostData($this->request->post());
        if(!$this->request->param("board_id")){
            return $this->error("缺少游戏id");
        }
        try {
            $count = $this->model->where('content_id',$post['content_id'])->where("board_id",$post['board_id'])->count();
            if($count>0){
                return $this->error('已添加该地图');
            }
            $data = [
                'content_id' => $post['content_id'],
                'board_id' => $post['board_id'],
                'sort' => $post['sort'],
            ];
            Db::startTrans();
            if ( $this->model->create($data)) {
                Db::commit();
                $this->clearCache();
                return $this->success('添加成功', $post);
            } else {
                Db::rollback();
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


}